package com.javabi.codegenerator.simple;

import java.io.InputStream;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import com.javabi.codegenerator.model.reader.ModelReadException;

public class SimpleResourceReader {

	public <T> T read(String resourceName, Class<T> type) {
		InputStream input = ClassLoader.getSystemResourceAsStream(resourceName);
		if (input == null) {
			throw new IllegalArgumentException("resournce not found: '" + resourceName + "'");
		}
		try {
			try {
				Serializer serializer = new Persister();
				T instance = serializer.read(type, input);
				return instance;
			} finally {
				input.close();
			}
		} catch (Exception e) {
			throw new ModelReadException(e);
		}
	}

}
